from django.shortcuts import render, redirect
# 导包
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
# 导入类视图
from django.views import View
from .models import *
from utils.captcha.captcha import captcha
from myapp.models import User
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
# 导入加密库
import hashlib
# 导入图片库
# 绘画库
from PIL import ImageDraw
# 字体库
from PIL import ImageFont
# 图片库
from PIL import Image
# 随机库
import random
# 文件流
import io

import requests

# 导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

# 导入原生sql模块
from django.db import connection

import jwt

# 导入redis数据库
import redis

# 导入时间模块
import time

# 导入公共目录变量
from mydjango.settings import BASE_DIR

# 导包
from django.db.models import Q, F

# 导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid
# 导入时间戳
from datetime import datetime
# 导入序列化对象
from myapp.myser import UserSer,GoodsSer, CategorySer

# 定义地址和端口
host = 'localhost'
port = 6379
# 建立redis连接
r = redis.Redis(host=host, port=port)


# 分类管理接口
class Cates(APIView):
    def get(self, request):
        # 展示
        cate = Category.objects.all()
        resp = {}
        resp["code"] = 200
        resp["data"] = CategorySer(cate, many=True).data
        print(resp)
        return Response(resp)

    def post(self, request):
        # 接收参数
        name = request.data.get('catename')
        # 排重
        cate = Category.objects.filter(cname=name).first()
        res = {}
        if cate:
            res['code'] = 405
            res['msg'] = '该分类已存在'
            return Response(res)
        # 入库
        category = Category(cname=name)
        category.save()
        res['code'] = 200
        res['msg'] = '添加分类成功'
        return Response(res)

    def delete(self, request):
        # 接收参数
        id = request.data.get('id')
        cate = Category.objects.filter(id=id).first()
        cate.delete()
        res = {}
        res['code'] = 200
        res['msg'] = '删除成功'
        return Response(res)

    def options(self, request):
        # 接收参数
        cid = request.data.get("id")
        catename = request.data.get('catename')
        cate = Category.objects.filter(id=cid).first()
        # 修改分类名称
        cate.cname = catename
        cate.save()
        res = {}
        res['code'] = 200
        res['msg'] = '修改成功'
        return Response(res)


# 商品列表接口
class GoodsList(APIView):
    def get(self, request):
        # 读取数据库
        goods = Goods.objects.all()
        # 序列化操作
        goods_ser = GoodsSer(goods, many=True)
        # 返回
        return Response(goods_ser.data)


# 商品入库接口
class AddGoods(APIView):
    def get(self, request):
        # 查询所有分类
        cate_list = Category.objects.all()

        cates = CategorySer(cate_list, many=True)
        return Response({'code': 200, 'cate_list': cates.data})

    def post(self, request):
        # 接收参数
        name = request.POST.get('name')
        desc = request.POST.get('desc')
        param = request.POST.get('param')
        cid = request.POST.get('cid')
        price = request.POST.get('price')
        # 接收文件
        img = request.FILES.get('img')
        video = request.FILES.get('video')

        # 商品排重
        goods = Goods.objects.filter(name=name).first()

        if goods:
            res = {}
            res['code'] = 405
            res['msg'] = '该商品已经存在'
            return Response(res)

        # 进行入库
        goods = Goods(name=name, desc=desc, img=img.name, video=video.name,
                      parameter=param, cid=cid, price=price)
        goods.save()

        res = {}
        res['code'] = 200
        res['msg'] = '商品添加成功'

        return Response(res)
